#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Linq;
using System.Collections.Generic;

namespace Flynn.Core.ExtensionMethods
{
    public static class UpdateSequence
    {
        public static bool IsVersion1YoungerThanVersion2(this IEnumerable<Update> updates, string version1, string version2)
        {
            if (string.IsNullOrEmpty(version1))
                throw new ArgumentNullException("version1");

            if (string.IsNullOrEmpty(version2))
                throw new ArgumentNullException("version2");

            int indexOfVersion1 = updates.IndexOf(version1);
            if (indexOfVersion1 == -1)
                throw new InvalidOperationException(string.Format("Version '{0}' not found.", version1));

            int indexOfVersion2 = updates.IndexOf(version2);
            if (indexOfVersion2 == -1)
                throw new InvalidOperationException(string.Format("Version '{0}' not found.", version2));

            return indexOfVersion1 > indexOfVersion2;
        }

        private static int IndexOf(this IEnumerable<Update> updates, string version)
        {
            int index = 0;

            foreach (Update update in updates)
            {
                if (update.Version == version)
                    return index;

                index++;
            }

            return -1;
        }
    }
}